package com.lakeworks.decision.handler;

import com.lakeworks.common.domain.channel.ticket.TicketAdvance;
import com.lakeworks.common.domain.ticket.TktDecision;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.common.enums.ticket.RouteKey;
import com.lakeworks.common.enums.ticket.TicketBackStatus;
import com.lakeworks.common.enums.ticket.TicketStatus;
import com.lakeworks.decision.AdvanceTicketDecision;
import com.lakeworks.decision.base.BaseDecisionHandler;
import com.lakeworks.decision.base.DecisionHandler;
import com.lakeworks.event.TicketUpdateStatusEvent;
import com.lakeworks.mapper.TktRouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component
public class AdvanceTicketDecisionHandler extends BaseDecisionHandler implements DecisionHandler<AdvanceTicketDecision> {

    private static ArrayList<String> routeList;
    static {
        routeList = new ArrayList<>();
        routeList.add(RouteKey.UPDATETICKET.name());
        routeList.add(RouteKey.UNDISCOVERED.name());
        routeList.add(RouteKey.ExFACTORY.name());
    }

    @Override
    public TktDecision apply(AdvanceTicketDecision decision) {
        TktTicket tktTicket = decision.getTktTicket();
        TicketAdvance advance = decision.getTicketAdvance();
        if(RouteKey.FINISHED.name().equals(advance.getEventType())){
            tktTicket.setStatus(TicketStatus.FINISHED.name());
            closeRelevanceTicket(tktTicket);
            updateTicketState(tktTicket);
        }else if(!routeList.contains(advance.getEventType())){
            tktTicket.setStatus(TicketStatus.WORKING.name());
            updateTicketState(tktTicket);
        }
        String eventType = decision.getTicketAdvance().getEventType();
        getLog().info("触发工单推动状态变更事件-----barcode:[{}],eventType:[{}]",tktTicket.getBarcode(),eventType);
        getApplicationContext().publishEvent(new TicketUpdateStatusEvent(this,tktTicket.getBarcode(),eventType));
        return super.getTktDecision(tktTicket, decision);
    }
}


